package bairui.international.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.model.domain.jpa.TB_ProductCategory;
import bairui.international.model.domain.jpa.TB_ProductProperty;
import bairui.international.model.domain.jpa.TB_ProductPropertyItem;
import bairui.international.model.domain.jpa.TB_WineBottleNorms;
import bairui.international.model.status.State_Product;
import bairui.international.service.Service_Product;
import bairui.international.service.Service_ProductCategory;
import bairui.international.service.Service_ProductPropertyItem;

/**
 * 
 * 【(物料)TB_Product】服务实现
 *
 * @generated
 * 
 * @see bairui.model.domain.jpa.TB_Product 模型
 * @see Service_Product 服务接口
 * @see BService_ProductImpl 服务实现基础
 *
 */
@Service
public class Service_ProductImpl extends BService_ProductImpl implements Service_Product {

	@Autowired private Service_ProductCategory service_ProductCategory;
	@Autowired private Service_ProductPropertyItem service_ProductPropertyItem;
	
	private TB_Product createInstance(
		String number, 
		String name, 
		Double price, 
		Double showPrice, 
		String detail, 
		Double score, 
		TB_ProductCategory category, 
		TB_WineBottleNorms wineBottleNorms,
		String imgUrl,
		Double divideRate,
		Double exchange
	){
		TB_ProductCategory rootCategory = service_ProductCategory.findRoot(category);
		//
		return dao.save(new TB_Product(
				new TB_Product.MustProperties() {
					@Override public State_Product getState() {
						return State_Product.t_create().to();
					}
					@Override public String getName() {
						return name;
					}
					@Override public TB_ProductCategory getProductCategory() {
						return category;
					}
					@Override public Double getPrice() {
						return price;
					}
					@Override public String getNumber() {
						return number;
					}
					@Override public TB_ProductCategory getRootCategory() {
						return rootCategory;
					}
					@Override public Double getScore() {
						return score;
					}
					@Override public Double getDivideRate() {
						return divideRate;
					}
					@Override public Double getExchange() {
						return exchange;
					}
				},
				new TB_Product.OptionalSingleProperties() {
					@Override public String getImgUrl() {
						return imgUrl;
					}
					@Override public String getDetail() {
						return detail;
					}
					@Override public String getComment() {
						return null;
					}
					@Override public TB_WineBottleNorms getWineBottleNormsWine() {
						return wineBottleNorms;
					}
					@Override public Double getShowPrice() {
						return showPrice;
					}
				}
			));
	}
	
	@Override
	public TB_Product savePriceProduct(long categoryId, TB_Product input) {
		input.setProductCategory(new TB_ProductCategory(categoryId));
		TB_Product bean = createInstance(
				input.getNumber(),
				input.getName(),
				input.getPrice(),
				input.getShowPrice(),
				null, 
				0D,
				new TB_ProductCategory(categoryId), 
				input.getWineBottleNormsWine(),
				input.getImgUrl(),
				input.getDivideRate(),
				input.getExchange()
			);
		bean.setWineBottleNormsWrap(input.getWineBottleNormsWrap());
		return bean;
	}
	
	@Override
	public TB_Product saveScoreProduct(long categoryId, TB_Product input) {
		input.setProductCategory(new TB_ProductCategory(categoryId));
		return createInstance(
				input.getNumber(),
				input.getName(),
				0D,
				0D,
				null, 
				input.getScore(),
				new TB_ProductCategory(categoryId), 
				input.getWineBottleNormsWine(),
				input.getImgUrl(),
				0D,
				0D
			);
	}

	@Override
	public void publish(long id) throws Exception {
		TB_Product bean = findById(id);
		bean.new UpdateProperties() {
			@Override public State_Product getState(TB_Product it) throws Exception {
				return it.getState().t_publish_from_Draft().to();
			}
		};
	}

	@Override
	public void down(long id) throws Exception {
		TB_Product bean = findById(id);
		bean.new UpdateProperties() {
			@Override public State_Product getState(TB_Product it) throws Exception {
				return it.getState().t_down_from_Uped().to();
			}
		};		
	}

	@Override
	public void up(long id) throws Exception {
		TB_Product bean = findById(id);
		bean.new UpdateProperties() {
			@Override public State_Product getState(TB_Product it) throws Exception {
				return it.getState().t_up_from_Downed().to();
			}
		};			
	}

	@Override
	public void excelImportSaves(long categoryId, List<TB_Product> inputs){
//		inputs.forEach(it->{
//			try {
//				// 编号
//				if(exists(it2->it2.number.eq(it.getNumber()))){
//					throw new ValidateObjectException("object", "", "产品[编号:"+it.getNumber()+"]已经存在");
//				}
//				// 产地
//				TB_Place place = it.getPlace();
//				if(null != place){
//					String placeName = place.getName();
//					if(null != placeName){
//						place = service_Place.findOne(it2->it2.name.eq(placeName));
//						if(null == place){
//							throw new ValidateObjectException("object", "", "产地[名称:"+placeName+"]不存在");
//						}else{
//							it.setPlace(place);
//						}
//					}
//				}
//				// 级别
//				TB_Level level = it.getLevel();
//				if(null != level){
//					String levelName = level.getName();
//					if(null != levelName){
//						level = service_Level.findOne(it2->it2.name.eq(levelName));
//						if(null == level){
//							throw new ValidateObjectException("object", "", "级别[名称:"+levelName+"]不存在");
//						}else{
//							it.setLevel(level);
//						}
//					}
//				}
//				// 容量规格
//				TB_Volume volume = it.getVolume();
//				if(null != volume){
//					String volumeName = volume.getName();
//					if(null != volumeName){
//						volume = service_Volume.findOne(it2->it2.name.eq(volumeName));
//						if(null == volume){
//							throw new ValidateObjectException("object", "", "容量规格[名称:"+volumeName+"]不存在");
//						}else{
//							it.setVolume(volume);
//						}
//					}
//				}
//				
//				
////				String placeName = it.getPlace().getName();
////				TB_Place place = service_Place.findOne(it2->it2.name.eq(placeName));
////				if(null == place){
////					throw new ValidateObjectException("object", "", "产地[名称:"+placeName+"]不存在");
////				}
////				//
////				it.setPlace(place);
//				save(categoryId, it);
//			} catch (Exception e) {
//				throw new RuntimeException(e);
//			}
//		});
	}
	
	@Override
	public void excelImportUpdate(List<TB_Product> inputs) {
//		inputs.forEach(it->{
//			try {
//				TB_Product bean = findOne(it2->it2.number.eq(it.getNumber()));
//				if(null == bean){
//					throw new ValidateObjectException("object", "", "物料[编号:"+it.getNumber()+"]不存在");
//				}
//				// 产地
//				TB_Place place = it.getPlace();
//				if(null != place){
//					String placeName = place.getName();
//					if(null != placeName){
//						place = service_Place.findOne(it2->it2.name.eq(placeName));
//						if(null == place){
//							throw new ValidateObjectException("object", "", "产地[名称:"+placeName+"]不存在");
//						}else{
//							bean.setPlace(place);
//						}
//					}
//				}
//				// 级别
//				TB_Level level = it.getLevel();
//				if(null != level){
//					String levelName = level.getName();
//					if(null != levelName){
//						level = service_Level.findOne(it2->it2.name.eq(levelName));
//						if(null == level){
//							throw new ValidateObjectException("object", "", "级别[名称:"+levelName+"]不存在");
//						}else{
//							bean.setLevel(level);
//						}
//					}
//				}
//				// 容量规格
//				TB_Volume volume = it.getVolume();
//				if(null != volume){
//					String volumeName = volume.getName();
//					if(null != volumeName){
//						volume = service_Volume.findOne(it2->it2.name.eq(volumeName));
//						if(null == volume){
//							throw new ValidateObjectException("object", "", "容量规格[名称:"+volumeName+"]不存在");
//						}else{
//							bean.setVolume(volume);
//						}
//					}
//				}
//				//
//				bean.setPrice(it.getPrice());
//				bean.setUnit(it.getUnit());
//				bean.setName(it.getName());
//				bean.setCost(it.getCost());
//				
//			} catch (Exception e) {
//				throw new RuntimeException(e);
//			}
//		});
	}

	@Override
	public void addPropertyItem(long id, TB_ProductPropertyItem pitem) {
		pitem = service_ProductPropertyItem.load(pitem);
		TB_ProductProperty p = pitem.getProductProperty();
		findById(id)
			.removeProductPropertyItems(p.getProductPropertyItems().toArray(new TB_ProductPropertyItem[]{}))
			.addProductPropertyItems(pitem);
	}

	@Override
	public TB_Product updateWithRootCategory(long id, TB_Product input) throws Exception {
		TB_Product bean = updateWithNonNullProperties(id, input);
		// 处理分类
		TB_ProductCategory category = bean.getProductCategory();
		if(null != category){
			category = service_ProductCategory.load(category);
			bean.setRootCategory(category.getRoot());
		}
		return bean;
	}

}
